Skip to content

Conversation

@sunha20
Copy link
Contributor

@sunha20 sunha20 commented Jan 28, 2026

🚀 이슈 번호

Resolve: {#2337}

🧩 문제 해결

스스로 해결: ✅ / ❌

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지 그리디
  • 🔹 어떤 방식으로 접근했는지
    각 팀별로, 방 A와 방 B의 차이를 계산해서 가장 큰 순으로 우선순위를 두고 이동 거리를 계산함

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(T * NlogN )
  • 이유:
    테스트케이스 개수 : T
    각 테스트케이스별로 정렬: N log N

💻 구현 코드

import java.io.*;
import java.util.Arrays;

public class Main {

  static int N, result;
  static int[] cnt = new int[3];
  static int[][] teams, weight;

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String[] temp;

    temp = br.readLine().split(" ");
    cnt[0] = Integer.parseInt(temp[0]);  //N
    cnt[1] = Integer.parseInt(temp[1]);  //A
    cnt[2] = Integer.parseInt(temp[2]);  //B
    N = cnt[0];

    while (cnt[0] != 0 || cnt[1] != 0 || cnt[2] != 0) {
      result=0;
      teams = new int[N][3]; // 풍선수, da, db
      weight = new int[N][4]; // 가중치, 더 가까운 방, index

      // 배열 채우기
      for (int n=0; n<N; n++) {
        temp = br.readLine().split(" ");
        teams[n][0] = Integer.parseInt(temp[0]);
        teams[n][1] = Integer.parseInt(temp[1]);
        teams[n][2] = Integer.parseInt(temp[2]);

        if (teams[n][1] < teams[n][2]) {
          weight[n][0] = teams[n][2] - teams[n][1];
          weight[n][1] = 1;
          weight[n][2] = 2;
        }
        else {
          weight[n][0] = teams[n][1] - teams[n][2];
          weight[n][1] = 2;
          weight[n][2] = 1;
        }
        weight[n][3] = n;
      }

      // 최소값
      // 가중치 정렬
      Arrays.sort(weight, (o1, o2) -> (o2[0] - o1[0]));

      for (int[] t: weight) {
        int near = t[1];
        int far = t[2];
        int i = t[3];
        int k = teams[i][0];
        int lenNear = teams[i][near];
        int lenFar = teams[i][far];

        if (cnt[near] > k) {
          result += lenNear * k;
          cnt[near] -= k;
        }
        else {
          result += (lenFar * (k-cnt[near])) + (lenNear * cnt[near]);
          cnt[far] -= (k-cnt[near]);
          cnt[near] = 0;
        }
      }

      System.out.println(result);


      // 다음 테케
      temp = br.readLine().split(" ");
      cnt[0] = Integer.parseInt(temp[0]);  //N
      cnt[1] = Integer.parseInt(temp[1]);  //A
      cnt[2] = Integer.parseInt(temp[2]);  //B
      N = cnt[0];
    }
  }
}

@sunha20 sunha20 self-assigned this Jan 28, 2026
@sunha20 sunha20 linked an issue Jan 28, 2026 that may be closed by this pull request
@sunha20 sunha20 changed the title 260128 : [BOJ 4716] 풍선 260128 : [BOJ 4716] 풍 Jan 30, 2026
@sunha20 sunha20 changed the title 260128 : [BOJ 4716] 풍 260128 : [BOJ 4716] 풍선 Jan 30, 2026
@sunha20 sunha20 merged commit caf1c45 into main Jan 30, 2026
3 checks passed
@sunha20 sunha20 deleted the sunha/2337/2 branch January 30, 2026 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260128 : 코딩테스트

2 participants